#!/bin/bash

# copy_file.sh
# This script copies a file to a specified directory if the file is newer than the existing one in that directory.
# Usage: ./copy_file.sh source_file target_directory

# Function to copy a file to the specified directory if it is newer
copy_file() {
    # Assign arguments to variables for better readability
    local source_file=$1
    local target_directory=$2

    # Check if the source file exists
    if [ ! -f "$source_file" ]; then
        echo "\"$source_file\" not found!"
        notify-send "\"$source_file\" not found!"
        return 1
    fi

    # Copy the file if it is newer than the one in the target directory
    if [ "$source_file" -nt "$target_directory/$(basename "$source_file")" ]; then
        \cp "$source_file" "$target_directory"
        echo "\"$source_file\" has been updated on $(LC_TIME=en_US.UTF-8 date +%Y-%m-%d-%H-%M-%S-%a)!"
    fi
}

# Check if the correct number of arguments is provided
if [ $# -ne 2 ]; then
    echo "Error: Incorrect number of arguments."
    echo "Usage: $0 source_file target_directory"
    exit 1
fi

# Call the copy_files function with all the passed arguments
copy_file "$@"

